package cn.ilikes.tools.hbase.jpa.metadatas.elements;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;

/**
 * The Class JoinTableMetadata.
 * 
 * @author Amresh Singh
 */
public class JoinTableMetadata {

	/** The join table name. */
	private String joinTableName;

	/** The join table schema. */
	private String joinTableSchema;

	/** The join columns. */
	private Set<String> joinColumns;

	/** The inverse join columns. */
	private Set<String> inverseJoinColumns;

	/**
	 * Instantiates a new join table metadata.
	 * 
	 * @param relationField
	 *                  the relation field
	 */
	public JoinTableMetadata(Field relationField) {
		JoinTable jtAnn = relationField.getAnnotation(JoinTable.class);

		setJoinTableName(jtAnn.name());
		setJoinTableSchema(jtAnn.schema());

		for (JoinColumn joinColumn : jtAnn.joinColumns()) {
			addJoinColumns(joinColumn.name());
		}

		for (JoinColumn inverseJoinColumn : jtAnn.inverseJoinColumns()) {
			addInverseJoinColumns(inverseJoinColumn.name());
		}
	}

	/**
	 * Gets the join table name.
	 * 
	 * @return the joinTableName
	 */
	public String getJoinTableName() {
		return joinTableName;
	}

	/**
	 * Sets the join table name.
	 * 
	 * @param joinTableName
	 *                  the joinTableName to set
	 */
	public void setJoinTableName(String joinTableName) {
		this.joinTableName = joinTableName;
	}

	/**
	 * Gets the join table schema.
	 * 
	 * @return the joinTableSchema
	 */
	public String getJoinTableSchema() {
		return joinTableSchema;
	}

	/**
	 * Sets the join table schema.
	 * 
	 * @param joinTableSchema
	 *                  the joinTableSchema to set
	 */
	public void setJoinTableSchema(String joinTableSchema) {
		this.joinTableSchema = joinTableSchema;
	}

	/**
	 * Gets the join columns.
	 * 
	 * @return the joinColumns
	 */
	public Set<String> getJoinColumns() {
		return joinColumns;
	}

	/**
	 * Adds the join columns.
	 * 
	 * @param joinColumn
	 *                  the joinColumns to add
	 */
	public void addJoinColumns(String joinColumn) {
		if (joinColumns == null || joinColumns.isEmpty()) {
			joinColumns = new HashSet<String>();
		}
		joinColumns.add(joinColumn);
	}

	/**
	 * Gets the inverse join columns.
	 * 
	 * @return the inverseJoinColumns
	 */
	public Set<String> getInverseJoinColumns() {
		return inverseJoinColumns;
	}

	/**
	 * Adds the inverse join columns.
	 * 
	 * @param inverseJoinColumn
	 *                  the inverseJoinColumns to add
	 */
	public void addInverseJoinColumns(String inverseJoinColumn) {
		if (inverseJoinColumns == null || inverseJoinColumns.isEmpty()) {
			inverseJoinColumns = new HashSet<String>();
		}

		inverseJoinColumns.add(inverseJoinColumn);
	}

}
